package de.cube3d.blitwit.test.cases 
{
	import de.cube3d.blitwit.render.BlitSpriteScene;
	import de.cube3d.blitwit.test.Test;
	import de.cube3d.blitwit.test.Util;
	import flash.geom.Point;
	import flash.display.Graphics;
	import flash.utils.getTimer;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.display.BitmapData;
	import de.cube3d.blitwit.render.*;
	import de.cube3d.blitwit.space.*;


	public class TestMovement extends Test
	{
		private var _moverBT:SpaceObject, _moverGS:SpaceObject;
		
		override protected function init():void {
			var bgspace:GridSpace = new GridSpace( -5000, -5000, 10000, 10000,200,200);
			var fgspace:BTSpace = new BTSpace ( -5000, -5000, 10000, 10000);
			bgspace.add(new BlitSpriteTexture(Util.getBitmap("tile_ground_grass"), -50000, -50000, 100000, 100000).setSortIndex(-100000));
			var sprites:Vector.<BitmapData> = new Vector.<BitmapData>();
			for (var i:int = 0; i < 10; i += 1) sprites.push(Util.getBitmap("fountain_" + i));
			
			fgspace.add(_moverBT = new BlitSpriteBitmap(Util.getBitmap("house_1_1"), 100, 100).setSortIndex(100));
			fgspace.add(new AnimatedBitmap(sprites, 94.25, 120.55, 20).setSortIndex(120));
			fgspace.add(new AnimatedBitmap(sprites, 94.25, 70.55, 20).setSortIndex(70));
			
			fgspace.add(_moverGS = new BlitSpriteBitmap(Util.getBitmap("house_1_1"), 100, 300).setSortIndex(100));
			fgspace.add(new AnimatedBitmap(sprites, 94.25, 320.55, 20).setSortIndex(320));
			fgspace.add(new AnimatedBitmap(sprites, 94.25, 270.55, 20).setSortIndex(270));
			
			fgspace.add(new BlitSpriteBitmap(Util.getBitmap("house_1_1"), 400, 300).setSortIndex(100));
			
			scene.append(bgspace);
			scene.append(fgspace);
			
			selectspaces = new <Space> [fgspace];
		}
		
		override public function renderTick(t:Number):void {
			_moverGS.x = _moverBT.x = Math.sin(t * 0.001) * 130 + 100;
			var y:Number = Math.cos(t * 0.001) * 50;
			_moverGS.y = y + 300;
			_moverBT.y = y + 100;
			_moverGS.sortIndex = _moverGS.y;
			_moverBT.sortIndex = _moverBT.y;
		}
		
		private var _dragObject:SpaceObject;
		private var _dragOffsetX:Number;
		private var _dragOffsetY:Number;
		override public function mouseDown(x:int, y:int):void {
			if (_selected != _moverBT && _selected != _moverGS && _selected) {
				_dragObject = _selected;
				_dragOffsetX = x;
				_dragOffsetY = y;
			}
		}
		
		override public function mouseMoved(x:int, y:int):void {
			super.mouseMoved(x, y);
			if (_dragObject != null) {
				_dragObject.x = _dragObject.x + x - _dragOffsetX;
				_dragObject.sortIndex = _dragObject.y = _dragObject.y + y - _dragOffsetY;
				_dragOffsetX = x;
				_dragOffsetY = y;
			}
		}
		
		override public function mouseUp (x:int, y:int):void {
			_dragObject = null;
		}
	}

}